using System;
using System.Data;
using System.Xml;

using OR.Reuse.Persistence;

using Derived = OR.Lazarus.Library.DataTier.Source;

namespace OR.Lazarus.Library.DataTier.Source.Lazarus
{
	/// <summary>
	/// Defines a Relationship between two Data Tables.
	/// </summary>
   public class RelationDetail : EntityBase
   {
		protected System.String mName;
		protected string mDescription;
	
		protected Derived.RelationReferenceDetail mSource;
		protected Derived.RelationReferenceDetail mTarget;

		public RelationDetail()
		{
	
			mSource = new Derived.RelationReferenceDetail();
			mSource.ParentEntity = this;
			mTarget = new Derived.RelationReferenceDetail();
			mTarget.ParentEntity = this;
		}
	
		/// <summary>
		/// Specifies a unique name for the entity.
		/// </summary>
		public System.String Name
		{
			get
			{
				return ( mName );
			}
			set
			{
				mName = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public Derived.RelationReferenceDetail Source
		{
			get
			{
				return ( mSource );
			}
			set
			{
				mSource = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public Derived.RelationReferenceDetail Target
		{
			get
			{
				return ( mTarget );
			}
			set
			{
				mTarget = value;
			}
		}

		/// <summary>
		/// Provides a long-text description for the entity.
		/// </summary>
		public string Description
		{
			get
			{
				return ( mDescription );
			}
			set
			{
				mDescription = value;
			}
		}

		public override void LoadXml( XmlElement pSource )
		{
			// Scalar Values
			mName = DataUtility.GetString( pSource, "name" );
			XmlElement vDescription = ( XmlElement )pSource.SelectSingleNode( "lz:description", PersistenceConfig.Namespaces );
			
			if ( vDescription != null )
			{
				mDescription = vDescription.InnerText;
			}
			

			// FK Values
			XmlElement vSource = ( XmlElement )pSource.SelectSingleNode( "lz:source", PersistenceConfig.Namespaces );
			
			if ( vSource != null )
			{
				mSource.LoadXml( vSource );
			}
			XmlElement vTarget = ( XmlElement )pSource.SelectSingleNode( "lz:target", PersistenceConfig.Namespaces );
			
			if ( vTarget != null )
			{
				mTarget.LoadXml( vTarget );
			}
		}
		
		public override XmlElement SaveXml( XmlDocument p_docContext )
		{
			XmlElement vNewElement	= p_docContext.CreateElement( "lz", "relation", "http://schemas.icitadel.com/Lazarus/Source.xsd" );

			// Scalar Values
			if ( !String.IsNullOrEmpty( mName ) )
			{
				vNewElement.SetAttribute( "name", mName );
			}
					
			if ( !String.IsNullOrEmpty( mDescription ) )
			{
				XmlElement	vDescription = p_docContext.CreateElement( "lz", "description", "http://schemas.icitadel.com/Lazarus/Source.xsd" );
				vDescription.InnerText = mDescription;
				vNewElement.AppendChild( vDescription );
			}
					

			// FK Values
			vNewElement.AppendChild( mSource.SaveXml( p_docContext ) );
			
			vNewElement.AppendChild( mTarget.SaveXml( p_docContext ) );
			

			return ( vNewElement );
		}

		public virtual object Clone()
		{
			Derived.RelationDetail v_oReturn = new Derived.RelationDetail();
			
			v_oReturn.Name = mName;
			v_oReturn.Description = mDescription;
			v_oReturn.Source = ( Derived.RelationReferenceDetail )mSource.Clone();
			v_oReturn.Target = ( Derived.RelationReferenceDetail )mTarget.Clone();
			
			// FK Values
			v_oReturn.Source = this.Source.CloneTyped();
			
			v_oReturn.Target = this.Target.CloneTyped();
			

			// Collections
			
			return ( v_oReturn );
		}

		public virtual Derived.RelationDetail CloneTyped()
		{
			return ( ( Derived.RelationDetail )this.Clone() );
		}
   }
}
   